#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/timer.h>
static struct timer_list my_timer;

//定时器回调函数，定时器中断触发后，执行该函数，5000ms一次，然后重设定时器
static void
my_timer_callback (struct timer_list *timer)
{
  printk (KERN_INFO "Timer callback function called [%lu].\n", jiffies);
  printk(KERN_INFO "Timer expired\n");
  mod_timer (&my_timer, jiffies + msecs_to_jiffies (5000));
}

static int __init
timer_init (void)
{
  printk (KERN_INFO "Module loaded\n");

  // >>> 在这里实现计数器
  // >>> Start timer after 5000 milliseconds (5 seconds)
  // 初始化定时器，
  timer_setup(&my_timer, my_timer_callback,0);
  // 设置定时器到期时间为当前时间加5秒
  my_timer.expires = jiffies + msecs_to_jiffies(5000);

  add_timer(&my_timer);


  return 0;
}

static void __exit
timer_exit (void)
{
  del_timer (&my_timer);
  printk (KERN_INFO "Module unloaded\n");
}

module_init (timer_init);
module_exit (timer_exit);

MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("Your Name");
MODULE_DESCRIPTION ("Module with timer");
